package client;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class ImageViewer extends JPanel implements MouseListener{
	private int width, height;
	private Image currentImage;
	private UploadGUI p;
	private Dimension imageDimension;
	private int bitsPerPixel;
	private BufferedImage bufferedImage;
	private boolean isMain;
	private JPanel parentPanel;
	private JLabel imageIdentifier;
	private JRadioButton selector;
	
	public ImageViewer(int width, int height, UploadGUI parent, boolean isMain, JPanel parentPanel, JLabel identifier, JRadioButton radioSelect){
		super();
		
		this.width = width;
		this.height = height;
		this.isMain = isMain;
		this.setLayout(new BorderLayout());
		this.p = parent;
		this.selector = radioSelect;
		this.parentPanel = parentPanel;
		this.imageIdentifier = identifier;
		this.addMouseListener(this);
		this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		this.setSize(width, height);
		this.setVisible(true);
	}
	
	public void setRadioSelection(JRadioButton r){
		selector = r;
	}
	
	public JRadioButton getRadioButton(){
		return selector;
	}
	
	public void setIdentifier(JLabel i){
		imageIdentifier = i;
	}
	
	public JLabel getImageIdentifier(){
		return imageIdentifier;
	}
	
	public JPanel getParentPanel(){
		return parentPanel;
	}
	
	public Dimension getPreferredSize(){
		return new Dimension(width, height);
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D)g;
		
		if(currentImage != null){
			g2.drawImage(currentImage, 0, 0, this);
		}
	}
	
	/** Causes the Image Viewer to re-draw the image viewer with a new image
	 *  passed to it as a buffered image
	 *  
	 *  @param b the buffered image to draw to the screen
	 */
	public void displayImage(BufferedImage b){
		bufferedImage = b;
		imageDimension = new Dimension(b.getWidth(), b.getHeight());
		bitsPerPixel = b.getColorModel().getPixelSize();
		currentImage = b.getScaledInstance(-width, height, Image.SCALE_DEFAULT);
		
		repaint();
	}

	public BufferedImage getBufferedImage(){
		return bufferedImage;
	}
	
	public int getBitsPerPixel(){
		return bitsPerPixel;
	}
	
	public Dimension getImageDimension(){
		return imageDimension;
	}
	
	public boolean isMain(){
		return isMain;
	}
	
	public void mouseClicked(MouseEvent e) {
		p.swap(this);
	}
	
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	public void mousePressed(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}
}
